<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School_room extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

	  $parent_id = 12;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "基础设置", "curr" => '/school_room', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", 'setting');

	}

	/**
	 * 教室管理
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function index($value='')
	{
		$this->load->model('Classo2o_model');
		$room_list = $this->Classo2o_model->get_classroom($this->user_data['school_id']);

		$this->smarty->assign("room_list", $room_list);
	
		$this->smarty->display("school_room/class_room_list.html");
	}

	/**
	 * 添加教室
	 * @param string $value [description]
	 */
	public function add($value='')
	{
		$this->smarty->assign("id", "0");
		$this->smarty->assign("post_url", "/school_room/add");

		//获取校区
		$this->load->model('Schoolo2o_model');
		$campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
		$this->smarty->assign("campus", $campus);

		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['name'])){
				$this->smarty->assign("name_err", "教室名称不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("name", $post_data['name']);
			}

			if(empty($post_data['total_number'])){
				$this->smarty->assign("total_number_err", "容纳人数不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("total_number", $post_data['total_number']);
			}
			//添加教室
			$this->load->model('Classo2o_model');
			$result = $this->Classo2o_model->add_classroom($post_data,$this->user_data['school_id']);
			header("Location:/school_room");exit;
		}


		$this->smarty->display("school_room/add.html");
	}

		/**
	 * 添加教室
	 * @param string $value [description]
	 */
	public function edit($id)
	{
		$this->smarty->assign("id", $id);
		$this->smarty->assign("post_url", "/school_room/edit/{$id}");
		
		//获取校区
		$this->load->model('Schoolo2o_model');
		$campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
		$this->smarty->assign("campus", $campus);

		//获取校区信息
		$this->load->model('Classo2o_model');
		$room = $this->Classo2o_model->get_classroom_by_id($id);
		if(empty($room)) {
			header("Location:/school_room");exit;
		}
		if($room['school_id'] != $this->user_data['school_id']){
			header("Location:/school_room");exit;
		}

		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['name'])){
				$this->smarty->assign("name_err", "教室名称不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("name", $post_data['name']);
			}

			if(empty($post_data['total_number'])){
				$this->smarty->assign("total_number_err", "容纳人数不能为空");
				$has_error = 1;
			}else{
				$this->smarty->assign("total_number", $post_data['total_number']);
			}
			//添加教室
			$this->load->model('Classo2o_model');
			$result = $this->Classo2o_model->edit_classroom($post_data,$id);
			header("Location:/school_room");exit;
		}

		$this->smarty->assign("name", $room['name']);
		$this->smarty->assign("total_number", $room['total_number']);
		$this->smarty->assign("campus_id", $room['campus_id']);


		$this->smarty->display("school_room/add.html");
	}

	/**
	 * 删除教室
	 * @param string $value [description]
	 */
	public function del($id)
	{	
		$this->smarty->assign("id", $id);
		
		$this->load->model('Classo2o_model');
		//获取校区信息
		$room = $this->Classo2o_model->get_classroom_by_id($id);
		if(empty($room)){
			header("Location:/school_room");exit;
		}elseif ($room['school_id']!=$this->user_data['school_id']) {
			header("Location:/school_room");exit;
		}

		//删除
		$this->Classo2o_model->delete_room($id,$this->user_data['school_id']);
		header("Location:/school_room");exit;
	}

}